package com.tedu.weibo.common.exception;

import com.tedu.weibo.common.result.JsonResult;
import com.tedu.weibo.common.result.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandlerRuntimeException(RuntimeException e){
        //添加对异常的日志记录操作
        log.error(e.getMessage(),e);
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        System.out.println("message:"+e.getFieldError().getDefaultMessage());
        log.error(e.getFieldError().getDefaultMessage(),e);
        return new JsonResult(StatusCode.VALIDATE_ERROR);
    }
    @ExceptionHandler
    public JsonResult doHandleIllegalArgumentException(IllegalAccessException e){
        log.error(e.getMessage(),e);
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @ExceptionHandler
    public JsonResult doHandleThrowable(Throwable e){
        log.error(e.getMessage(),e);
        return new JsonResult(StatusCode.UNKNOWN_ERROR);
    }
}
